package com.hzdl.hotel.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzdl.hotel.bean.*;
import com.hzdl.hotel.service.*;
import com.hzdl.hotel.util.JSONResult;
import com.hzdl.hotel.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@RequestMapping("user")
@Controller
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private AdviceService adviceService;
    @Autowired
    private RoomOrderService roomOrderService;
    @Autowired
    private RoomBookService roombookService;
    @Autowired
    private RoomService roomService;

    /**
     * 登录初始化
     * @return
     */
    @RequestMapping("loginInit")
    public String loginInit(){
        return "user/login";
    }

    /**
     * 酒店浏览
     * @return
     */
    @RequestMapping("hotelInit")
    public String hotelInit(){
        return "user/hotel";
    }

    /**
     *退出登录消除session
     * @param session
     * @return
     */
    @RequestMapping("reloadInit")
    public String reloadInit(HttpSession session){
        if (session.getAttribute("user") != null) {
            session.removeAttribute("user");
        }
        return "user/login";
    }

    /**
     * 注册初始化
     * @return
     */
    @RequestMapping("registerInit")
    public String registerInit(){
        return "user/register";
    }

    /**
     * 首页初始化
     * @return
     */
    @RequestMapping("index")
    public String index(Model model,RoomBookVo roomBookVo){
        model.addAttribute("msg","请点击留言簿的首页进行查看!");
        List<RoomBook> allRoom = roomService.findAllRoom(roomBookVo);
        model.addAttribute("allRoom",allRoom);
        return "user/index";
    }

    /**
     * 房间浏览
     * @return
     */
    @RequestMapping("homeShowInit")
    public String homeShowInit(Model model){
        model.addAttribute("msg","请点击首页查看剩余的房间");
        return "user/rooms";
    }

    /**
     * 公告浏览
     * @return
     */
    @RequestMapping("newShowInit")
    public String newShowInit(){
        return "user/news";
    }

    /**
     * 服务浏览
     * @return
     */
    @RequestMapping("serviceShowInit")
    public String serviceShowInit(){
        return "user/service";
    }

    /**
     * 联系我们
     * @return
     */
    @RequestMapping("contactInit")
    public String contactInit(){
        return "user/contact";
    }

    /**
     * 关于我们
     * @return
     */
    @RequestMapping("aboutusInit")
    public String aboutusInit(){
        return "user/about-us";
    }

    /**
     * 修改订单初始化
     * @return
     */
    @RequestMapping("orderUpdateInit")
    public String orderUpdateInit(String checkId , HttpServletRequest request){
        RoomBook roomBook = roombookService.findBycheckId(checkId);
        request.setAttribute("roomBook",roomBook);
        return "user/orderUpdate";
    }

    /**
     * 预定房间
     * @return
     */
    @RequestMapping("roomOrder")
    public String roomOrder(String hId,Model model){
        RoomOrder roomOrder = roomOrderService.findByhId(Integer.valueOf(hId));
        model.addAttribute("roomOrder",roomOrder);
        return "user/roomOrder";
    }

    /**
     * 修改预定房间
     * @return
     */
    @RequestMapping("orderChk")
    public String orderChk(){
        return "user/orderChk";
    }

    /**
     * 登录
     * @param user
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("login")
    public String login(User user, Model model, HttpSession session){
        User user1 = userService.findByNameAndId(user);
        if (user1 == null){
            model.addAttribute("msg","账号或者密码错误");
            return "user/login";
        }else {
            session.setAttribute("user",user1);
            model.addAttribute("msg","请点击首页查看剩余的房间");
            return "user/rooms";
        }
    }

    /**
     * 修改密码
     * @return
     */
    @RequestMapping("changePwdInit")
    public String changePwd(){
            return "user/changePwd";
    }

    /**
     * 注册用户
     * @param req
     * @param resp
     * @param model
     * @return
     */
    @RequestMapping("register")
    public String register(HttpServletRequest req, HttpServletResponse resp , Model model){
        String loginId =req.getParameter("loginId");
        String loginPwd =req.getParameter("loginPwd");
        String loginName =req.getParameter("loginName");
        String address =req.getParameter("address");
        String phone =req.getParameter("phone");
        String sex =req.getParameter("sex");

        User user = new User();
        user.setLoginId(loginId);
        user.setLoginPwd(loginPwd);
        user.setLoginName(loginName);
        user.setAddress(address);
        user.setPhone(phone);

        if (sex != null && !sex.isEmpty()){
            user.setSex(Integer.parseInt(sex));
        }

        int row = userService.registerUser(user);
        if (row != 0){
            return "redirect:/user/loginInit";
        }else {
            model.addAttribute("msg","注册失败");
            return "user/register";
        }
    }

    /**
     * 用户建议的添加
     * @param advice
     * @param model
     * @return
     */
    @RequestMapping("addAdvice")
    public String addAdvice(Advice advice,Model model){
        int i = adviceService.addAdvice(advice);
        if (i==0){
            model.addAttribute("msg","抱歉,您的建议未能成功提交,请稍后再试");
            return "user/contact";
        }else {
            model.addAttribute("msg","非常感谢您对本店提出的宝贵建议!");
            return "user/contact";
        }
    }

    /**
     * 房间显示和分页
     * @param page
     * @param model
     * @param roomOrderVo
     * @return
     */
    @RequestMapping("roomShow")
    public String roomShow(@RequestParam(value = "page", defaultValue = "1") Integer page, Model model,RoomOrderVo roomOrderVo){
        roomOrderVo.setLimit(2);
        if (roomOrderVo.getPage() ==null){
            roomOrderVo.setPage(1);
        }
        PageHelper.startPage(page, roomOrderVo.getLimit());
        List<RoomOrder> roomOrders = roomOrderService.findByWhere(roomOrderVo);
        PageInfo pageInfo = new PageInfo(roomOrders);
        model.addAttribute("pageInfo", pageInfo);
        return "user/rooms";
    }

    /**
     * 分类的房间显示和分页
     * @param page
     * @param model
     * @param guestRoomVo
     * @return
     */
    @RequestMapping("eachRoomShow")
    public String eachRoomShow(@RequestParam(value = "page", defaultValue = "1") Integer page, Model model, GuestRoomVo guestRoomVo , String type){
        guestRoomVo.setLimit(2);
        if (guestRoomVo.getPage() ==null){
            guestRoomVo.setPage(1);
        }
        PageHelper.startPage(page, guestRoomVo.getLimit());
        List<GuestRoom> eachRoom = roomOrderService.findByType(type);
        model.addAttribute("type",type);
        PageInfo pageInfo = new PageInfo(eachRoom);
        model.addAttribute("pageInfo", pageInfo);
        return "user/eachRooms";
    }




    /**
     * 评论显示和分页
     * @param page
     * @param model
     * @param adviceVo
     * @return
     */
    @RequestMapping("adviceShow")
    public String adviceShow(@RequestParam(value = "page", defaultValue = "1") Integer page, Model model, AdviceVo adviceVo){
        adviceVo.setLimit(1);
        PageHelper.startPage(page, adviceVo.getLimit());
        List<Advice> advice = adviceService.findAll(adviceVo);
        PageInfo pageInfo = new PageInfo(advice);
        model.addAttribute("pageInfo", pageInfo);
        return "user/index";
    }

    /**
     * 房间的预定
     * @param roomBook
     * @param model
     * @return
     */
    @RequestMapping("roomBook")
    public String roomBook(RoomBook roomBook,Model model,String hId){
        String checkId="SH_";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        checkId+=sdf.format(new Date());
        checkId+="_";
        Random r = new Random();
        for(int i=0;i<6;i++){
            checkId+=r.nextInt(10);
        }
        checkId+="_";
        for(int i=0;i<3;i++){
            checkId+=r.nextInt(10);
        }
        roomBook.setCheckId(checkId);
        int i = roombookService.addRoomBook(roomBook);
        int j = roombookService.updateRoomRenting(hId);
        if (i==0 || j==0){
            model.addAttribute("msg","预定房间出现了一些错误,请稍后再试");
            return "user/roomOrder";
        }else {
            model.addAttribute("msg","预定成功");
            return "user/orderChk";
        }

    }

    /**
     * 个人订单的显示分页
     * @param page
     * @param model
     * @param roomBookVo
     * @param session
     * @return
     */
    @RequestMapping("orderShow")
    public String orderShow(@RequestParam(value = "page", defaultValue = "1") Integer page, Model model, RoomBookVo roomBookVo,HttpSession session){
        Object obj = session.getAttribute("user");
        if (obj==null ){
            model.addAttribute("msg","请登录账号");
            return "user/login";
        }
        User user = (User)obj;
        roomBookVo.setLimit(5);
        PageHelper.startPage(page, roomBookVo.getLimit());

        List<RoomBook> roomBooks = roombookService.findAll(user.getUserId());
        PageInfo pageInfo = new PageInfo(roomBooks);
        model.addAttribute("pageInfo", pageInfo);
        return "user/orderChk";
    }

    /**
     * 删除订单
     * @param checkId
     * @return
     */
    @RequestMapping("delOrder")
    @ResponseBody
    public JSONResult delOrder(String checkId){
        int i = roombookService.delOrder(checkId);
        if (i==0){
            return new JSONResult(0,"删除失败");
        }else {
            return new JSONResult(-1,"删除成功");
        }
    }

    /**
     * 房间预定订单的修改
     * @param model
     * @param roomBook
     * @return
     */
    @RequestMapping("roomOrderUpdate")
    public String roomOrderUpdate(Model model,RoomBook roomBook){

        int i = roombookService.updateOrder(roomBook);
        if (i==0){
            model.addAttribute("msg","更新失败");
        }else {
            model.addAttribute("msg","更新成功");
        }
        return "user/orderChk";
    }

    /**
     * 修改密码
     * @param model
     * @param userVo
     * @return
     */
    @RequestMapping("changePwd")
    public String changePwd(Model model, UserVo userVo){
        User user1 = userService.findByLoginId(userVo);
        if (user1.getUserId()==0){
            try {
                model.addAttribute("msg","账号或密码错误,请稍后再试");
            }catch (Exception e){
                e.printStackTrace();
            }
            return "user/changePwd";
        }else {
            int i = userService.changePwd(userVo);
            if (i==0){
                model.addAttribute("msg","修改失败,请稍后再试");
                return "user/changePwd";
            }else {
                model.addAttribute("msg","修改成功");
                return "user/login";
            }
        }

    }

}
